package com.whately.core.base.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.IntegerCodec;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.whately.core.base.utils.GsonUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页返回vo
 */
@Slf4j
@ApiModel(value = "分页结果")
@ToString
public class PageResponseBO<T> {
    private static final long serialVersionUID = -6835515582292217174L;

    @ApiModelProperty(value = "当前页码")
    private int page;
    @ApiModelProperty(value = "单页数量")
    private int size;

    @ApiModelProperty(value = "总数")
    @JSONField(serializeUsing = IntegerCodec.class)
    //@JsonAdapter(value = GsonIntegerTypeAdapter.class)
    private long total;

    @ApiModelProperty(value = "总页数")
    @JSONField(serializeUsing = IntegerCodec.class)
    //@JsonAdapter(value = GsonIntegerTypeAdapter.class)
    private long totalPage;

    private List<T> list = new ArrayList();

    public PageResponseBO() {
    }

    public long getTotal() {
        return this.total;
    }

    public void setTotal(long total) {
        this.total = total;
        this.dealTotalPage();
    }

    private void dealTotalPage() {
        if(this.total > 0 && this.size > 0) {
            long totalPage = this.total % this.size == 0?this.total / this.size:this.total / this.size + 1;
            this.setTotalPage(totalPage);
        }

    }

    public int getPage() {
        return this.page;
    }

    public void setPage(int page) {
        this.page = page;
        this.dealTotalPage();
    }

    public List<T> getList() {
        return this.list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getSize() {
        return this.size;
    }

    public void setSize(int size) {
        this.size = size;
        this.dealTotalPage();
    }

    public long getTotalPage() {
        return this.totalPage;
    }

    public void setTotalPage(long totalPage) {
        this.totalPage = totalPage;
    }

    public static <T> PageResponseBO<T> changeFromJson(String json ,Class<T> cls){
        String list_property_name="list";
        PageResponseBO<T> bo = null;
        try{
            if (StringUtils.isNotBlank(json)) {
                JsonObject gJsonObject = new JsonParser().parse(json).getAsJsonObject();

                List<T>  dataList = GsonUtils.jsonToList(gJsonObject.get(list_property_name).toString(),cls);

                gJsonObject.remove(list_property_name);

                bo = GsonUtils.getDefaultGson().fromJson(gJsonObject,PageResponseBO.class);

                bo.setList(dataList);

                /*
                JavaType javaType = ObjectMapperUtil.getCollectionType(cls);
                JSONObject objJSON = JSON.parseObject(json);

                //1.先把list转换过了
                JSONArray listJo = objJSON.getJSONArray(list_property_name);
                List<T> ls = new ArrayList<>();
                for(int i=0;i<listJo.size();i++){
                    Object jo = listJo.get(i);
                    String m =jo.toString();
                    ls.add(ObjectMapperUtil.getObjectMapper().readValue(m,javaType));//强制类型转换
                }

                //2.去掉list后，转其它的部分
                objJSON.remove(list_property_name);
                bo = objJSON.toJavaObject(PageResponseBO.class);
                //3.重新设置第一步转换的list
                bo.setList(ls);
                 */
            }
        }catch (Exception e){
            log.error("changeFromJson error",e);
            throw new RuntimeException("changeFromJson error");
        }
        return bo;
    }

}
